﻿using Dapper;
using Inventorycount.API.Read.Application.Command;
using Inventorycount.Domain;
using Inventorycount.Dto;
using Inventorycount.ErrorCode;
using MediatR;
using System.Data;
using System.Text;

namespace Inventorycount.API.Read.Application.CommandHandLer
{
    public class GetUserCommandHandLer : IRequestHandler<GetUserCommand, APIPaing<Wms_User>>
    {
        private readonly IDbConnection repository;

        public GetUserCommandHandLer(IDbConnection repository)
        {
            this.repository = repository;
        }
        public async Task<APIPaing<Wms_User>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            var result = new APIPaing<Wms_User>
            {
                Code = ResultCode.Ok,
                Message = "查询成功",
                Data = new List<Wms_User>()
            };

            try
            {
                // 构建基础查询
                var sql = new StringBuilder(@"
                    SELECT 
                        Id,
                        Name ,
                        DepaName
                    FROM Wms_User ");

                var parameters = new DynamicParameters();


                // 执行查询
                var goods = await repository.QueryAsync<Wms_User>(sql.ToString(), parameters);

                result.Data = goods.ToList();
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.Fail;
                result.Message = $"查询失败: {ex.Message}";
            }

            return result;
        }
    }
}
